#include "Code5_DBL.h"

unsigned char DipMode = 0;	

#ifdef LED
const uint16_t Led[LED_NUM] = {LED1, LED2, LED3, LED4, LED5, LED6, LED7};		
#endif										
	 
#ifdef DIP
const uint16_t Dip[DIP_NUM] = {DIP1, DIP2, DIP3, DIP4, DIP5, DIP6};					
#endif		

const uint16_t Btn[BTN_NUM] = {BTN1, BTN2, BTN3, BTN4, BTN5, BTN6};
Bool Btn_Pressed_Past[BTN_NUM] = {0};																																	 
Bool Btn_Pressed_Now[BTN_NUM] = {0};		
Bool Btn_Triggered[BTN_NUM] = {0};
				 
unsigned char ReadDip(void)
{			
	unsigned char dip_in = 0;
	int i;
	
	#ifdef DIP
	for(i=0; i < DIP_NUM; i++)
	{
		dip_in += (!GPIO_ReadInputDataBit(DIP_PORT, Dip[i])) * (1 << i); 
	}	
	#endif

	return dip_in;
}

void LedSwitch(WorkStatus status, int i)
{
	i--;

	#ifdef LED
	if((i >= 0) && (i < LED_NUM))
	{
		if(status == ON)
		{
			GPIO_SetBits(LED_PORT , Led[i]);
		}
		else
		{
			GPIO_ResetBits(LED_PORT , Led[i]);
		}
	}
	#endif
}

Bool GetLedStatus(int i)
{
	i--;

	#ifdef LED
	if((i >= 0) && (i < LED_NUM))
	{
		return GPIO_ReadOutputDataBit(LED_PORT , Led[i]);
	}
	#endif
	return FALSE;
}

void ReadBtn(void)
{
	int i = 0;
	for(i=0; i < BTN_NUM; i++)
	{
		Btn_Pressed_Now[i] = !GPIO_ReadInputDataBit(GPIOF, Btn[i]);

		if(Btn_Pressed_Now[i] == FALSE && Btn_Pressed_Past[i] == TRUE)
		{
			Btn_Triggered[i] = TRUE;
		}
		else
		{
			Btn_Triggered[i] = FALSE;
		}
	}															

	for(i=0; i < BTN_NUM; i++)
	{
		Btn_Pressed_Past[i] = Btn_Pressed_Now[i];
	}
}